package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Sound;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
@Slf4j
public class SoundApiImpl implements SoundApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 将语音存入Mongodb
     * @param sound
     * @return
     */
    @Override
    public String saveSound(Sound sound) {
        this.mongoTemplate.save(sound);
        return sound.getUserId().toString();
    }

    /**
     * 根据Id查询语音
     * @param userId
     * @return
     */
    @Override
    public List<Sound> querySoundById(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<Sound> soundList = this.mongoTemplate.find(query, Sound.class);
        return soundList;
    }

    /**
     * 查询所有语音
     * @return
     */
    @Override
    public List<Sound> querySounds() {
        List<Sound> soundList = this.mongoTemplate.findAll(Sound.class);
        return soundList;
    }
}
